package cn.iocoder.yudao.module.system.controller.admin.userordermanage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.userordermanage.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userordermanage.UserOrderManageDO;
import cn.iocoder.yudao.module.system.service.userordermanage.UserOrderManageService;

@Tag(name = "管理后台 - 订单管理")
@RestController
@RequestMapping("/system/user-order-manage")
@Validated
public class UserOrderManageController {

    @Resource
    private UserOrderManageService userOrderManageService;

    @PostMapping("/create")
    @Operation(summary = "创建订单管理")
    @PreAuthorize("@ss.hasPermission('system:user-order-manage:create')")
    public CommonResult<Long> createUserOrderManage(@Valid @RequestBody UserOrderManageSaveReqVO createReqVO) {
        return success(userOrderManageService.createUserOrderManage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单管理")
    @PreAuthorize("@ss.hasPermission('system:user-order-manage:update')")
    public CommonResult<Boolean> updateUserOrderManage(@Valid @RequestBody UserOrderManageSaveReqVO updateReqVO) {
        userOrderManageService.updateUserOrderManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-order-manage:delete')")
    public CommonResult<Boolean> deleteUserOrderManage(@RequestParam("id") Long id) {
        userOrderManageService.deleteUserOrderManage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-order-manage:query')")
    public CommonResult<UserOrderManageRespVO> getUserOrderManage(@RequestParam("id") Long id) {
        UserOrderManageDO userOrderManage = userOrderManageService.getUserOrderManage(id);
        return success(BeanUtils.toBean(userOrderManage, UserOrderManageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单管理分页")
    @PreAuthorize("@ss.hasPermission('system:user-order-manage:query')")
    public CommonResult<PageResult<UserOrderManageRespVO>> getUserOrderManagePage(@Valid UserOrderManagePageReqVO pageReqVO) {
        PageResult<UserOrderManageDO> pageResult = userOrderManageService.getUserOrderManagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserOrderManageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单管理 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-order-manage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserOrderManageExcel(@Valid UserOrderManagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserOrderManageDO> list = userOrderManageService.getUserOrderManagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单管理.xls", "数据", UserOrderManageRespVO.class,
                        BeanUtils.toBean(list, UserOrderManageRespVO.class));
    }

}